<?php
/**
 * Created by PhpStorm.
 * User: jyolo
 * Date: 2017/2/5
 * Time: 16:08
 */

namespace app\base\controller;
use app\base\library\Auth;
use app\base\model\AdminLog;
use think\Controller;
use think\Exception;
use think\Db;
use think\facade\Session;


class Login extends Controller
{

    //登录
    public function in(){
        if ($this->request->isGet()){

            if(Session::get('manager_login') !== 'pass'){
                return $this->fetch();
            }else{

                $this->redirect('index/index');
            }

        }

        if ($this->request->isPost()){
            $post = input('post.');


            $validate = new \app\base\validate\Login();
            $flag = $validate->check($post);


            if(!$flag) $this->error($validate->getError());
            //验证账户的正确性
            $manager = $validate->check_isright_manager($post);
            if(!$manager)$this->error($validate->getError());

            $auth = new Auth();
            //获取角色的权限
            $role = $auth->get_role_auth($manager['role_id']);
            if(!$role) $this->error($auth->getError());





            //载入角色信息
            Session::set('role',$role);
            Session::set('manager_login','pass');
            Session::set('manager',$manager);


            AdminLog::create();


            $this->success('登录成功',url('index/index'));
        }

    }
    //退出
    public function out(){
        if ($this->request->isPost()) {
            $post = input('post.');
            if($post['action'] === 'logout'){
                AdminLog::create();
                Session::clear();
                $this->success('退出成功',url('base/login/in'));
            }
        }

    }
}